﻿using System.Linq;

using vurfu.DataContracts;
using vurfu.Linq;

namespace vurfu.Repositories
{
    public class RatingsRepository
    {
        public Ratings GetRatings(string id)
        {
            Ratings ratings;
            using(var connection = DataContextFactory.Create())
            {
                ratings = (from ratingsRecord in connection.GetTable<Ratings>()
                           where ratingsRecord.Id == id
                           select ratingsRecord).First();
            }
            return ratings;
        }

        public void SaveRatings(Ratings ratings)
        {
            using(var connection = DataContextFactory.Create())
            {
                connection.GetTable<Ratings>().InsertOnSubmit(ratings);
                connection.SubmitChanges();
            }
        }

        public void ChangeRatings(Ratings ratings)
        {
            using (var connection = DataContextFactory.Create())
            {
                var oldRatings = (from ratingsRecord in connection.GetTable<Ratings>()
                           where ratingsRecord.Id == ratings.Id
                           select ratingsRecord).First();
                
                oldRatings.Authority = ratings.Authority;
                oldRatings.Emotional = ratings.Emotional;
                oldRatings.Quality = ratings.Quality;
                oldRatings.Trust = ratings.Trust;

                connection.SubmitChanges();
            }
        }
    }

}